from google.appengine.ext import db
import hashlib


class Classification(db.Model):
    name = db.StringProperty()
    iconcolor = db.StringProperty()
    description = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    
    def __str__(self):
        return 'name: %s\n description: %s' % (self.name, self.description)
    
class Message(db.Model):
    author = db.UserProperty()
    message = db.TextProperty()
    marked_message = db.TextProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    last_update = db.DateTimeProperty(auto_now = True)
    classification = db.ReferenceProperty(Classification)
    
    def __str__(self):
        #return 'message: %s\n created_on: %s\n author: %s\n classification: %s' % (self.message, self.created_on, self.author.email, self.classification.name)
        return 'message: %s\n classification: %s' % (self.message, self.classification.name)
        #return 'message: %s' % self.message
    
    def md5(self):
        return hashlib.md5(self.message).hexdigest()

    def htmlMsg(self):
        htmlMessage = self.message.replace("&", "&amp;")
        htmlMessage = htmlMessage.replace(">", "&gt;")
        htmlMessage = htmlMessage.replace("<", "&lt;")
        htmlMessage = htmlMessage.replace("\n", "\n<br/>")
        htmlMessage = htmlMessage.replace(" ", "&nbsp;")
        return htmlMessage

class User(db.Model):
    user = db.UserProperty()
    iconcolor = db.StringProperty()
    description = db.StringProperty()
    access_count = db.IntegerProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    last_update = db.DateTimeProperty(auto_now = True)
    
    def __str__(self):
        return 'user: %s\n description: %s' % (self.nickname, self.description)